package com.ganmiao.server.sys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ganmiao.server.sys.entity.SysUserRole;
import com.ganmiao.server.sys.query.SysUserRoleQuery;
import com.ganmiao.server.sys.vo.SysUserRoleVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户角色信息
 */
@Mapper
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

    /**
     * 新增用户角色信息 (关联用户)
     *
     * @param sysUserRole
     * @return
     */
    @Insert({
            "<script>",
            "insert into sys_user_role (user_id,role_id) values",
            "<foreach collection = 'sysUserRole.userIds' item = 'userIds' separator = ','>",
            "(#{userIds},#{sysUserRole.roleId})",
            "</foreach>",
            "</script>"
    })
    boolean addSysUserRole(@Param("sysUserRole") SysUserRole sysUserRole);

    /**
     * 新增用户角色信息 (用户关联角色)
     *
     * @param sysUserRoles
     * @return
     */
    @Insert({
            "<script>",
            "insert into sys_user_role (user_id,role_id) values",
            "<foreach collection = 'list' item = 'sysUserRole' separator = ','>",
            "(#{sysUserRole.userId},#{sysUserRole.roleId})",
            "</foreach>",
            "</script>"
    })
    boolean addSysUserRoleByUserId(List<SysUserRole> sysUserRoles);

    /**
     * 获取用户角色信息 (已关联用户)
     *
     * @param iPage
     * @param sysUserRoleQuery
     * @return
     */
    @Select({
            "<script>",
            "select user_role.role_id,user.user_id,user.user_name,user.user_phone,user.status,user.created_time",
            "from sys_user user join sys_user_role user_role on user.user_id = user_role.user_id",
            "where user.sys_code = #{sysUserRoleQuery.sysCode} ",
            "and user_role.role_id = #{sysUserRoleQuery.roleId} ",
            "and user.status = 1 and user_role.status = 1 ",
            "and user.user_id in (select user_id from sys_user_role where role_id = #{sysUserRoleQuery.roleId} and status = '1')",
            "<if test = 'sysUserRoleQuery.userName!=null'>",
            "and user.user_name like concat('%',#{sysUserRoleQuery.userName},'%')",
            "</if>",
            "<if test = 'sysUserRoleQuery.supplierId!=null'>",
            "and user.user_id in (select user_id from supplier_user where supplier_id = #{sysUserRoleQuery.supplierId} and status = 1)",
            "</if>",
            "<if test = 'sysUserRoleQuery.shopId!=null'>",
            "and user.user_id in (select user_id from shop_user where shop_id = #{sysUserRoleQuery.shopId} and status = 1)",
            "</if>",
            "order by user_role.created_time desc",
            "</script>"
    })
    IPage<SysUserRoleVo> getSysUserRoleListExist(@Param("iPage") IPage<SysUserRoleVo> iPage,@Param("sysUserRoleQuery") SysUserRoleQuery sysUserRoleQuery);

    /**
     * 获取用户角色信息 (未关联用户)
     *
     * @param iPage
     * @param sysUserRoleQuery
     * @return
     */
    @Select({
            "<script>",
            "select user_id,user_name,user_phone,status,created_time",
            "from sys_user",
            "where status = 1",
            "and user_id not in (select user_id from sys_user_role where role_id = #{sysUserRoleQuery.roleId} and status = '1') ",
            "and sys_code = #{sysUserRoleQuery.sysCode}",
            "<if test = 'sysUserRoleQuery.userName!=null'>",
            "and user_name like concat('%',#{sysUserRoleQuery.userName},'%')",
            "</if>",
            "<if test = 'sysUserRoleQuery.supplierId!=null'>",
            "and user_id in (select user_id from supplier_user where supplier_id = #{sysUserRoleQuery.supplierId} and status = 1)",
            "</if>",
            "<if test = 'sysUserRoleQuery.shopId!=null'>",
            "and user_id in (select user_id from shop_user where shop_id = #{sysUserRoleQuery.shopId} and status = 1)",
            "</if>",
            "order by created_time desc",
            "</script>"
    })
    IPage<SysUserRoleVo> getSysUserRoleListNotExist(@Param("iPage")IPage<SysUserRoleVo> iPage,@Param("sysUserRoleQuery") SysUserRoleQuery sysUserRoleQuery);

}
